rm(list =ls())
options(scipen=999)
gc()
packages <-c("tidyverse","estimatr","plm","stargazer",
             "fastDummies","ICCbin","ihs","readstata13","xtable")

new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages)

setwd("PUT YOUR DIRECTORY HERE")

# load data
data <- read.csv("./Datasets/main_confounders.csv")

# cleaning / scaling
data <- data%>%
  dplyr::mutate(across(subbotnik_inkind_1975:subbotnik_inkind_1979, ~scale(.x)))%>%
  dplyr::rowwise()%>%
  dplyr::mutate(compliance = mean(c(subbotnik_inkind_1975, subbotnik_inkind_1976, 
                                    subbotnik_inkind_1977, subbotnik_inkind_1978,
                                    subbotnik_inkind_1979), na.rm = TRUE))%>%
  dplyr::ungroup()%>%
  dplyr::mutate(commander_1975_86 = scale(commander_1975_86),
                strikes = scale(strikes),
                sabotage = scale(compliance * -1))


lm_strikes <- lm(strikes ~ commander_1975_86 + arrests, data = data)
lm_sabotage <- lm(sabotage ~ commander_1975_86 + arrests, data = data)

#save in tex
stargazer(lm_strikes, lm_sabotage,
          title = "Table A10: Effect of surveillance on resistance (controlling for arrests)",
          dep.var.labels = c("Protests", "Sabotage"),
          style = "qje",
          covariate.labels = c("Surveillance","Arrests"),
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01),
          omit = c("Constant", "factor"),
          omit.stat = c("rsq", "f", "ser"), 
          omit.table.layout = "n",
          add.lines = list(c("Fixed effects", "No", "No"),
                           c("Controls", "No", "No")),
          out = "PUT YOUR FILEPATH HERE")
